package javango.contrib.i18n;

/**
 * Static Translation tool that uses a ThreadLocal provider
 * 
 * @author john
 * 
 */
public class Translation {

	private static ThreadLocal<I18NProvider> threadLocalI18NProvider = new ThreadLocal<I18NProvider>() {

		@Override
		public I18NProvider get() {
			return super.get();
		}

		public void set(I18NProvider arg0) {
			super.set(arg0);
		}
	};

	/**
	 * Sets the ThreadLocal I18NProvider.
	 * 
	 * @param provider
	 */
	public static void setI18NProvider(I18NProvider locale) {
		threadLocalI18NProvider.set(locale);
	}

	public static I18NProvider getI18NProvider() {
		if (threadLocalI18NProvider.get() == null) {
			throw new NullPointerException(
					"I18NProvider not set,  please call Translation.setI18NProvider");
		}
		return threadLocalI18NProvider.get();
	}

	public static I18NString getLazy(String string, Object... args) {
		return new I18NStringImpl(getI18NProvider(), string, args);
	}

	public static String getText(String string, Object... args) {
		return getI18NProvider().getText(string, args);
	}

	public static String getText(String string) {
		return getI18NProvider().getText(string);
	}

	public static String _(String string, Object... args) {
		return getI18NProvider().getText(string, args);
	}

	public static I18NString _lazy(String string, Object... args) {
		return new I18NStringImpl(getI18NProvider(), string, args);
	}

	public static I18NString _lazy(String string) {
		return new I18NStringImpl(getI18NProvider(), string, null);
	}

}
